/*
 * @(#)UTCDateUtils.java
 * Date 2013-09-02
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.utils;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-02
 * @author         Jim Horner
 */
public class UTCDateUtils {

    /** Field description */
    public static final String DATE_FORMAT = "yyyyMMdd";

    /** Field description */
    public static final String[] DATE_FORMATS = new String[] { "yyyyMMdd", "yyyyMM",
            "yyyy" };

    /** Field description */
    public static final String DATE_TIME_FORMAT = "yyyyMMddHHmmss";

    /** Field description */
    public static final String[] DATE_TIME_FORMATS = new String[] {

        "yyyyMMddHHmmssZ", "yyyyMMddHHmmZ", "yyyyMMddHHZ", "yyyyMMddZ", "yyyyMMZ",
        "yyyyZ", "yyyyMMddHHmmss", "yyyyMMddHHmm", "yyyyMMddHH", "yyyyMMdd", "yyyyMM",
        "yyyy"
    };

    /** Field description */
    public static final String ISO_DATE_FORMAT = "yyyy-MM-dd";

    /** Field description */
    public static final String ISO_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";

    /** Field description */
    private static final DatatypeFactory dataTypeFactory;

    static {

        try {

            dataTypeFactory = DatatypeFactory.newInstance();

        } catch (DatatypeConfigurationException e) {

            throw new IllegalStateException("Unable to create DataTypeFactory.");
        }

    }

    /**
     * Method description
     *
     *
     * @param date
     * @param format
     *
     * @return
     */
    public static String formatDate(DateTime date, String format) {

        return DateTimeFormat.forPattern(format).withZoneUTC().print(date);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public static DateTime now() {

        return DateTime.now(DateTimeZone.UTC);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public static XMLGregorianCalendar nowXMLGregorianCalendar() {

        DateTime now = now();

        return dataTypeFactory.newXMLGregorianCalendar(now.toGregorianCalendar());
    }

    /**
     * Method description
     *
     *
     * @param date
     *
     * @return
     */
    public static DateTime parseDate(String date, String format) {

        return parseDate(date, new String[] { format });
    }

    /**
     * Method description
     *
     *
     * @param date
     * @param formats
     *
     * @return
     */
    public static DateTime parseDate(String date, String[] formats) {

        DateTime result = null;

        for (String format : formats) {

            try {

                DateTimeFormatter fmt = DateTimeFormat.forPattern(format).withZoneUTC();

                result = fmt.parseDateTime(date);

                if (result != null) {

                    break;
                }

            } catch (Exception e) {

                // do nothing
            }
        }

        return result;
    }
}
